package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.AdminController;
import com.zhangzeqin.code.entity.Admin;

import javax.swing.*;
import javax.swing.event.MenuDragMouseListener;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.SQLException;

public class AdminView extends JFrame {
    public static void main(String[] args) {
        new AdminView();
    }

    JLabel nameLabel = new JLabel("公告", JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    /**
     * "公告"文本区域,5行20列,"发布按钮"
     */
    JTextArea noticeField=new JTextArea(5,20);
    JButton publishButton=new JButton("发布");
    /**
     * 创建菜单栏容器
     */
    JMenuBar menuBar = new JMenuBar();
    SystemTray systemTray;
    TrayIcon trayIcon;
    AdminHandler adminHandler;

    JMenu user = new JMenu("用户");
    JMenu word = new JMenu("单词");
    JMenu help=new JMenu("帮助");
    /**
     * 创建"帮助"的子菜单
     */
    JMenuItem exitMenuItem=new JMenuItem("退出");
    JMenu modifyWord=new JMenu("单词管理");
    JMenuItem addBook=new JMenuItem("添单词书");
    JMenuItem userManager=new JMenuItem("用户管理");

    /**
     * 管理员控制层
     */
    AdminController adminController=new AdminController();
    Object[] arr=adminController.nature();
    //标记单词书
    static String whichBook=null;
    //循环数
    int i=0;

    public AdminView() {
        super("管理员界面");
        adminHandler=new AdminHandler(this);
        //获取内容面板
        Container contentPane=getContentPane();
        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        nameLabel.setPreferredSize(new Dimension(0,80));
        Font  centerFont=new Font("楷体",Font.PLAIN,20);
        publishButton.setFont(centerFont);
        //"公告"文本框设置自动换行
        noticeField.setLineWrap(true);
        //初始化文本区域
        try {
            noticeField.setText((String) adminController.getNotice());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        //添加到面板
        centerPanel.add(noticeField);
        centerPanel.add(publishButton);
        //菜单栏大小
        menuBar.setSize(900, 60);
        menuBar.setBounds(0, 0, 900, 60);

        //一级菜单添加到菜单栏
        menuBar.add(user);
        menuBar.add(word);
        menuBar.add(help);

        //子菜单添加到一级菜单
        help.add(exitMenuItem);
        user.add(userManager);
        word.add(modifyWord);
        word.add(addBook);

        //将数组的元素加到二级菜单栏
        JMenuItem book1=new JMenuItem((String) arr[0]);
        JMenuItem book2=new JMenuItem((String) arr[1]);
        JMenuItem book3=new JMenuItem((String) arr[2]);
        JMenuItem book4=new JMenuItem((String) arr[3]);
        JMenuItem book5=new JMenuItem((String) arr[4]);
        JMenuItem book6=new JMenuItem((String) arr[5]);
        modifyWord.add(book1);
        modifyWord.add(book2);
        modifyWord.add(book3);
        modifyWord.add(book4);
        modifyWord.add(book5);
        modifyWord.add(book6);

        //弹性布局
        springLayout.putConstraint(SpringLayout.WEST,noticeField,190,SpringLayout.WEST,nameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,publishButton,40,SpringLayout.SOUTH,noticeField);
        springLayout.putConstraint(SpringLayout.WEST,publishButton,70,SpringLayout.WEST,noticeField);
        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);


        //把菜单栏设置到窗口
        setJMenuBar(menuBar);
        //增加系统托盘
        addTray();


        //自定义图标
        URL imgUrl = LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600, 400);
        //计算位置居中
        setLocationRelativeTo(null);
        //关闭退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        //可视
        setVisible(true);

        //各个菜单栏监听器
        myListener(book1, book2, book3, book4, book5, book6);
    }

    private void myListener(JMenuItem book1, JMenuItem book2, JMenuItem book3, JMenuItem book4, JMenuItem book5, JMenuItem book6) {
        //添加用户管理(子菜单)监听器
        userManager.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UserTableView();
            }
        });
        //添加单词管理(子菜单)监听器
        modifyWord.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ListWordView();
            }
        });
        //添加单词书(子菜单)监听器
        addBook.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ListBookView();
            }
        });
        //添加退出(子菜单)监听器
        exitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginView();
            }
        });
        //"发布"监听
        publishButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String notice = noticeField.getText();
                boolean flag = false;
                if (notice.length() == 0) {
                    JOptionPane.showMessageDialog(null, "不能为空！");
                } else {
                    flag = adminController.publishNotice(notice);
                    if(flag){
                        JOptionPane.showMessageDialog(null, "发布成功！");
                    }
                }
            }
        });


        //单词书监听器
        book1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                whichBook= (String) arr[0];
                new ListWordView();
            }
        });
        book2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                whichBook= (String) arr[1];
                new ListWordView();
            }
        });
        book3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                whichBook= (String) arr[2];
                new ListWordView();
            }
        });
        book4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                whichBook= (String) arr[3];
                new ListWordView();
            }
        });
        book5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                whichBook= (String) arr[4];
                new ListWordView();
            }
        });
        book6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                whichBook= (String) arr[5];
                new ListWordView();
            }
        });
    }


    /**
     * 增加系统托盘
     */
    private void addTray() {
        if (SystemTray.isSupported()) {
            systemTray = SystemTray.getSystemTray();
            URL imgUrl = LoginView.class.getClassLoader().getResource("tray.png");
            trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
            //设置托盘图片大小自动缩放
            trayIcon.setImageAutoSize(true);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //增加最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {

                @Override
                public void windowIconified(WindowEvent e) {
                    AdminView.this.dispose();
                }
            });
            //托盘事件监听
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        AdminView.this.setExtendedState(JFrame.NORMAL);
                    }
                    AdminView.this.setVisible(true);
                }
            });
        }
    }

}